package com.xiaodou.mjf.web.xface.core.threads;

import com.xiaodou.mjf.application.core.manager.AccountManager;
import com.xiaodou.mjf.application.user.manager.MemberManager;
import com.xiaodou.mjf.application.user.manager.result.RegisterResult;
import com.xiaodou.mjf.application.user.utils.MemberUtils;
import com.xiaodou.mjf.entity.core.Account;
import com.xiaodou.mjf.entity.core.NotesBook;
import com.xiaodou.mjf.entity.user.Member;
import com.xiaodou.mjf.infrastructure.common.context.SpringContextHolder;
import com.xiaodou.mjf.infrastructure.common.sms.SMSClient;
import com.xiaodou.mjf.infrastructure.common.utils.StringUtils;
import com.xiaodou.mjf.infrastructure.payment.RZContents;

/**
 * Created by Rocky on 15/3/27.
 */
public class OpenningAccountThread extends Thread {


    MemberManager memberManager = SpringContextHolder.getBean(MemberManager.class);
    AccountManager accountManager = SpringContextHolder.getBean(AccountManager.class);



    String mobile;
    String channelId;
    Double amount;
    String orderId;

    public OpenningAccountThread(String mobile,String channelId,Double amount,String orderId){
        this.mobile = mobile;
        this.channelId = channelId;
        this.amount = amount;
        this.orderId = orderId;
    }

    @Override
    public void run() {

        Member member = MemberUtils.getMember(mobile);

        if(member == null){
            String password = StringUtils.substring(mobile,mobile.length() - 6);
            RegisterResult result = memberManager.register(mobile, password);
            if(result.isSuccess()){
                SMSClient.sendNewUserMessage(mobile,"http://jr.mayfle.com");
                Account account = MemberUtils.getAccount(result.getUid());
                accountManager.deposit(account,channelId,amount, NotesBook.TYPE_DEPOSIT,"在线支付", RZContents.DEFAULT_PAYMENT_CHANNEL_ID,orderId);
                accountManager.transferOut(account, channelId, amount, NotesBook.TYPE_ONLINE_PAYMENT, "在线支付");
            }
        }

    }
}
